package com.cos.emp.services.impl;

import com.cos.emp.dao.EmpMapper;
import com.cos.emp.entity.Emp;
import com.cos.emp.services.EmpServices;
import com.cos.emp.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/9/30 14:20
 * @description:
 */
@Service
@Transactional
public class EmpServicesImpl implements EmpServices {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public boolean updateEmp(Emp emp) {
        if (empMapper.updateEmp(emp)>0) {
            return true;
        }
        return false;
    }
    @Override
    public Emp getEmpByEno(Integer eno) {
        if (eno>0) {
            return empMapper.getEmpByEno(eno);
        }
        return null;
    }

    @Override
    public boolean addEmp(Emp emp) {
        if (empMapper.addEmp(emp)>0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean checkName(String ename) {
        if (StringUtil.isNotEmpty(ename)) {
            if (empMapper.getEmpByEname(ename) != null) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean delEmp(Integer eno) {
        if (eno > 0) {
            if (empMapper.delEmp(eno) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public PageInfo<Emp> getEmpByPage(Map<String, String> emps) {
        // 获取 转换 当前页面
        int num = StringUtil.stringToInt(emps.get("pageNum"));
        // 创建pageHelper
        PageHelper.startPage(num, 5);
        List<Emp> all = empMapper.getEmpByKey(emps);
        PageInfo<Emp> info = new PageInfo<>(all);
        return info;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public Emp doLogin(String ename, String pwd) {
        if (StringUtil.isNotEmpty(ename, pwd)) {
            Emp emp = empMapper.getEmpByEname(ename);
            if (emp != null && emp.getPwd().equals(pwd)) {
                return emp;
            }
        }
        return null;
    }

    @Override
    public List<Emp> getAll() {
        return empMapper.getAll();
    }
}
